package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.beans.Expression;
import java.util.List;

public class UserContext {
    public static final String USER_IN_SESSION="USER_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION="EXPRESSION_IN_SESSION";

    //获取session对象
    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
        HttpSession session = requestAttributes.getRequest().getSession();
        return session;
    }

    public static Employee getCurrentUser(){
        return  (Employee)getSession().getAttribute(USER_IN_SESSION);
    }

    public static void setCurrentUser(Employee employee){
        // RequestContextHolder工具可以在任意地方拿到请求对象和响应对象
        HttpSession session =getSession();
        session.setAttribute(USER_IN_SESSION,employee);
    }

    public static List<String> getExpression(){
        return ( List<String>)getSession().getAttribute(EXPRESSION_IN_SESSION);
    }

    public static void setExpression(List<String> expressions){
        HttpSession session = getSession();
        session.setAttribute(EXPRESSION_IN_SESSION,expressions);
    }
}
